package com.weihui.jxc.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.weihui.jxc.common.Constants;
import com.weihui.jxc.common.ErrorManager;
import com.weihui.jxc.model.Goods;
import com.weihui.jxc.model.InAndOut;
import com.weihui.jxc.model.InAnOutInfo;

/**
 * 入库
 * @author xiaowei
 *
 */
public class OutportDao implements BaseDao<InAndOut> {
	SqlManager sqlManager;

	public OutportDao() {
		sqlManager = SqlManager.createInstance();
		sqlManager.connectDB();
	}

	/**
	 * 添加数据
	 */
	@Override
	public boolean save(InAndOut i) {
		// 编写sql
		String sql = "insert into t_outport values(?,?,?,?,?,?,?,?,?,?)";
		// 参数设置
		Object[] params = new Object[] { i.getId(), i.getNumber(), i.getPrice(), i.getComment(), i.getProviderId(),
				i.getInporttime(), i.getOperateperson(), i.getSponsor(), i.getPaytype(), i.getGoodsId() };
		boolean isFlag = sqlManager.executeUpdate(sql, params, Constants.PSTM_TYPE);
		sqlManager.closeDB();
		return isFlag;
	}

	/**
	 * 动态生成编号
	 */
	@Override
	public String getId() {
		// 获取最新的id
		String sql = "select MAX(id) id from t_outport";
		// 执行sql
		ResultSet rs = sqlManager.executeQuery(sql, null, Constants.PSTM_TYPE);
		String id = "RK1001";
		try {
			if (rs != null && rs.next()) {
				String sid = rs.getString("id");// 获取前一次插入的id
				if (sid != null) {
					String str = sid.substring(2);// 截取编号中的数字
					id = "RK" + (Integer.parseInt(str) + 1);
				}
			}
		} catch (SQLException e) {
			ErrorManager.printError("CustomerDao getCustomerId()", e);
		}
		sqlManager.closeDB();
		return id;
	}

	@Override
	public List<InAndOut> query(InAndOut t) {
		// TODO Auto-generated method stub
		return null;
	}
    /**
     * 查询入库信息
     * @param i
     * @return
     */
	public List<InAnOutInfo> queryAll(InAnOutInfo i) {
		List<InAnOutInfo> list = null;
		String sql = null;
		Object[] params = null;
		if(i==null) {//全部查询
			sql ="select i.*,g.goodsname,p.provider_name from t_outport i,t_goods g,"
					+ "t_provider p where i.goods_id = g.id and i.provider_id = p.id";
		}else {
			if(i.getId()!=null && i.getId().length()>0) {//根据入库单号查询
				sql="select i.*,g.goodsname,p.provider_name from t_outport i,t_goods g,t_provider p " + 
						"  where i.goods_id = g.id and i.provider_id = p.id and i.id =?";
				params = new Object[] {i.getId()};
			}else if(i.getProviderName()!=null && i.getProviderName().length()>0) {//根据供应商名字查询
				sql ="select i.*,g.goodsname,p.provider_name from t_outport i,t_goods g,t_provider p " + 
						"  where i.goods_id = g.id and i.provider_id = p.id and p.provider_name like ?";
				params = new Object[] {"%"+i.getProviderName()+"%"};
			}else if(i.getGoodsName()!=null && i.getGoodsName().length()>0) {//根据商品名称查询
				sql ="select i.*,g.goodsname,p.provider_name from t_outport i,t_goods g,t_provider p " + 
						"  where i.goods_id = g.id and i.provider_id = p.id and g.goodsname like ?";
				params = new Object[] {"%"+i.getGoodsName()+"%"};
			}else if(i.getSponsor()!=null && i.getSponsor().length()>0) {//根据经手人查询
				sql ="select i.*,g.goodsname,p.provider_name from t_outport i,t_goods g,t_provider p " + 
						"  where i.goods_id = g.id and i.provider_id = p.id and i.sponsor like ?";
				params = new Object[] {"%"+i.getSponsor()+"%"};
			}else if(i.getStartTime()!=null && i.getEndTime()!=null) {//根据日期查询
				sql ="select i.*,g.goodsname,p.provider_name from t_outport i,t_goods g,t_provider p " + 
						"  where i.goods_id = g.id and i.provider_id = p.id and inporttime between ? and ? ";
				params = new Object[] {i.getStartTime(),i.getEndTime()};
			}
		}
		// 执行sql
		ResultSet rs = sqlManager.executeQuery(sql, params, Constants.PSTM_TYPE);
		// 解析结果集
		if (rs != null) {
			list = new ArrayList<InAnOutInfo>();
			try {
				while (rs.next()) {
					InAnOutInfo cus = new InAnOutInfo();
					cus.setId(rs.getString("id"));
					cus.setNumber(rs.getInt("number"));
					cus.setPrice(rs.getFloat("price"));
					cus.setComment(rs.getString("comment"));
					cus.setProviderId(rs.getString("provider_id"));
					cus.setInporttime(rs.getDate("inporttime"));
					cus.setOperateperson(rs.getString("operateperson"));
					cus.setSponsor(rs.getString("sponsor"));
					cus.setPaytype(rs.getString("paytype"));
					cus.setGoodsId(rs.getString("goods_id"));
					cus.setProviderName(rs.getString("provider_name"));
					cus.setGoodsName(rs.getString("goodsname"));
					list.add(cus);
				}
			} catch (SQLException e) {
				ErrorManager.printError("InportDao queryAll()", e);
			}
		}
		return list;
	}
	@Override
	public boolean update(InAndOut t) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean update(String id, Integer available) {
		// TODO Auto-generated method stub
		return false;
	}

}